package org.budo.github.api;

import org.budo.github.api.response.GithubAccessTokenResponse;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.java.net.util.UrlEncodeUtil;

/**
 * @author lmw
 */
public class GithubOauthApi extends AbstractGithubApi {
    public GithubAccessTokenResponse accessToken(String code) {
        String url = "https://github.com/login/oauth/access_token" //
                + "?client_id=" + this.getAppKey() //
                + "&client_secret=" + this.getAppSecret() //
                + "&code=" + code//
                + "&redirect_uri=" + UrlEncodeUtil.encode(this.getRedirectUri());

        Request tokenRequest = new Request();
        tokenRequest.setMethod(Request.POST);
        tokenRequest.setUrl(url);
        Response tokenResponse = tokenRequest.execute();
        return new GithubAccessTokenResponse(tokenResponse);
    }
}